package com.hotel.po.controller;

import com.hotel.common.utils.Result;
import com.hotel.po.service.HotelService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/po/hotels")
public class HotelController {

    @Autowired
    private HotelService hotelService;

    @GetMapping
    public Result<Map<String, Object>> list(@RequestParam(name = "page", defaultValue = "1") int page,
                                            @RequestParam(name = "size", defaultValue = "10") int size,
                                            @RequestParam(name = "search", required = false) String search) {
        PageInfo<Map<String, String>> pageInfo;
        if (search != null && !search.trim().isEmpty()) {
            pageInfo = hotelService.search(search.trim(), page, size);
        } else {
            pageInfo = hotelService.list(page, size);
        }
        java.util.HashMap<String, Object> resp = new java.util.HashMap<>();
        resp.put("list", pageInfo.getList());
        resp.put("total", pageInfo.getTotal());
        return Result.success(resp);
    }

    @GetMapping("/{id}")
    public Result<java.util.Map<String, String>> detail(@PathVariable("id") Long id) {
        return Result.success(hotelService.detail(id));
    }

    @PostMapping
    public Result<Long> create(@RequestBody Map<String, Object> body) {
        return Result.success(hotelService.create(body));
    }

    @PutMapping
    public Result<Void> update(@RequestBody Map<String, Object> body) {
        hotelService.update(body);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable("id") Long id) {
        hotelService.delete(id);
        return Result.success();
    }
}


